package com.pic.others.service.impl;

import com.github.benmanes.caffeine.cache.Cache;
import com.pic.common.autoconfigure.mq.RabbitMqHelper;
import com.pic.common.constants.MqConstants;
import com.pic.common.constants.RedisConstants;
import com.pic.common.utils.R;
import com.pic.common.utils.UserContext;
import com.pic.others.domain.po.Bottle;
import com.pic.others.domain.po.Notice;
import com.pic.others.mapper.BottleMapper;
import com.pic.others.mapper.NoteMapper;
import com.pic.others.mapper.NoticeMapper;
import com.pic.others.mq.message.BottleComsumerMessage;
import com.pic.others.mq.message.BottleMessage;
import com.pic.others.mq.message.EmailMessage;
import com.pic.others.service.IBottleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;

import static com.pic.others.constants.user.UserConstants.USER_VERIFY_CODE_KEY_REGISTER;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author koshi
 * @since 2023-11-24
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class BottleServiceImpl extends ServiceImpl<BottleMapper, Bottle> implements IBottleService {

    private final StringRedisTemplate redisTemplate;
    private final RabbitMqHelper mqHelper;
    private final NoticeMapper noticeMapper;
    private final NoticeServiceImpl noticeService;
    Cache<String, Object> caffeineCache;

    @Override
    public R<String> addBottle(String sendContent) {
        log.info("sendContent:{}",sendContent);
        long userId = UserContext.getUser();
//        Bottle bottle=new Bottle();
//        bottle.setBottleContent(context);
//        bottle.setCreateTime(LocalDateTime.now());
//        bottle.setSendId(userId);
        mqHelper.send(
                MqConstants.Exchange.BOTTLE_EXCHANGE,
                MqConstants.Key.BOTTLESENSITIVE,
                BottleMessage.of(sendContent,String.valueOf(userId))
        );
        return R.success("发送成功，谢谢你，让世界上的另一个人感受到了爱");
    }

    @Override
    public R<String> getBottle() {
        // 从Zset中随机获取漂流瓶的ID
        String randomBottleId = (String) redisTemplate.opsForZSet().randomMember(RedisConstants.bottle.BOTTLE_ID);
        if(randomBottleId==null){
            return R.success("没有漂流瓶了，下次再来吧！");
        }
        log.info(randomBottleId);
        Bottle bottle = getById(randomBottleId);


        mqHelper.send(
                MqConstants.Exchange.BOTTLE_EXCHANGE,
                MqConstants.Key.BOTTLE_HANDLER,
                BottleComsumerMessage.of(String.valueOf(UserContext.getUser()),randomBottleId)
        );
        return R.success(bottle.getBottleContent());
    }

    @Override
    public R<Notice> getNoticeByAuto() {
        Long userId = UserContext.getUser();
        if(userId==null){
            return R.error("请先登录");
        }
        Notice notice = noticeService.lambdaQuery().eq(Notice::getUserId, userId).one();
        if(notice==null){
            R.success("不需要推送");
        }
        String content=notice.getContent();
        noticeService.removeById(notice.getNoticeId());
        return R.success(notice);
    }
}
